<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="选择商品">
          <el-radio v-model="radio" label="1">全部商品</el-radio>
          <el-radio v-model="radio" label="2">部分商品</el-radio>
          <el-cascader
            v-if="radio === '2'"
            v-model="form.config"
            placeholder="请选择商品"
            :options="selectData"
            clearable
            collapse-tags
            :props="{ expandTrigger: 'hover', checkStrictly: true, multiple: true }"
          />
        </el-form-item>

        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" :loading="loading" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getGoods } from '@/api/product'
import { statementCreate } from '@/api/stock'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      selectData: [],
      radio: '1',
      loading: false,
      rules: {
        config_id: [
          { required: true, message: '请选择商品状态', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getGoods()
  },
  methods: {
    /** 初始：查询商品 */
    getGoods() {
      const that = this
      getGoods().then(response => {
        if (response.code === 10000) {
          that.selectData = JSON.parse(JSON.stringify(response.data))
          that.selectData.forEach((item) => {
            item.label = item.goods_name
            item.value = item.goods_name
          })
        }
      })
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      that.loading = true
      var data = {}
      if (that.radio === '1') {
        data.is_all_product = true
        data.goods_ids = []
      } else {
        data.is_all_product = false
        data.goods_ids = []
        that.selectData.forEach((item) => {
          that.form.config.forEach((item2) => {
            if (item2 == item.goods_name) {
              data.goods_ids.push(item.goods_id)
            }
          })
        })
      }
      console.log(data)
      statementCreate(data).then(response => {
        console.log(response)
        that.$message.success(response.message)
        that.$parent.getList()
        that.$emit('update:open', false)
      }).finally(() => {
        that.loading = false
      })
    },
    edit() {
      const that = this
      that.loading = true
      inventoryUpdate(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      }).finally(() => {
        that.loading = false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    }
  }
}
</script>

